import Enity from "./Enity.js"
import Bullet from "./Bullet.js";
import { boo,playerImg,hpPropImg } from "../image.js";
//玩家类
class PlayEnity extends Enity {
    type = 'player'
    name = '玩家'
    img;//所使用的图片
    v;//移动速度
    hp;//玩家生命值
    canHitedTarget = [];//会遭受到哪些实体的撞击

    time = 0;//存活时间
    buttelRate = 10;//子弹发射速度
    fireStatus = 1;//发射状态

    //参数
    timer=null;//内置定时器
    constructor(x, y,canHitedTarget = ['enemy']) {
        super(x, y, 20)
        this.img = {
            body:playerImg,
            hp:hpPropImg,
            boo
        }
        this.canHitedTarget = canHitedTarget
        this.v = 5
        this.hp = 3;

    }
    //显示图片
    draw = function (ctx) {
        this.drawBody(ctx)//绘制玩家
        this.drawHp(ctx)//绘制血条
    }
    drawBody = function(ctx){
        if(this.hp>0){
            ctx.drawImage(this.img.body, this.x-this.R, this.y-this.R, this.R * 2, this.R * 2)
        }else{
            ctx.drawImage(this.img.boo[this.time%5], this.x - this.R, this.y - this.R, this.R * 2, this.R * 2)
        }
    }
    drawHp = function(ctx){
        //绘制hp血条
        let i=this.hp;
        let positionHp = {
            x:-25,
            y:10
        };//血条的初始位置
        while(i>0){
            positionHp.x += 35
            let w =this.img.hp.width
            let h =this.img.hp.height
            if(i-1>=0){
                ctx.drawImage(this.img.hp,positionHp.x,positionHp.y,30,28)
                i--;
            }else{
                ctx.drawImage(this.img.hp,0,0,w*i,h,positionHp.x,positionHp.y,30*i,28)
                i=0;
            }
        }
    }
    //行为
    behavior = function(context){
        this.time++
        this.move(context)
        if(this.hp>0) context = this.fire(context)//开火
        context = this.hit(context)
        return context
    }
    //碰撞判断
    hit = function(context){
        context.NodeList.forEach(target=>{
            if(this.canHitedTarget.includes(target.type)){
                if(this.nodeDistance(target)<=target.R+this.R-10 && target.hp){
                    let hp = this.hp;
                    this.hp -= target.hp;
                    if(this.hp<0) {this.hp = 0;hp=0}//为了防止敌人会碰到尸体爆炸
                    target.hp -= hp
                    if(target.hp) target.hp = 0
                }
            }
        })
        return context
    }
    //移动
    move = function (context) {
        //hp来决定移动方式
        if(this.hp>0){
            if (Math.abs(this.x - context.mouseX) < 5 && Math.abs(this.y - context.mouseY) < 5) {
                return
            }
            let long = ((this.x - context.mouseX) ** 2 + (this.y - context.mouseY) ** 2) ** 0.5
            let vx = -this.v * ((this.x - context.mouseX) / long)
            let vy = -this.v * ((this.y - context.mouseY) / long)
            this.x += vx;
            this.y += vy;
        }
    }
    //发射子弹
    fire(context){
        switch(this.fireStatus){
            case 1 :{
                if(this.time%this.buttelRate==0){
                    context.NodeList.push(new Bullet(this.x,this.y))
                }
                break;
            }
            case 2 :{
                if(this.time%this.buttelRate==0){
                    context.NodeList.push(new Bullet(this.x,this.y,Math.PI/20))
                    context.NodeList.push(new Bullet(this.x,this.y,-Math.PI/20))
                }
                break;
            }
            case 3 :{
                if(this.time%this.buttelRate==0){
                    context.NodeList.push(new Bullet(this.x,this.y))
                    context.NodeList.push(new Bullet(this.x,this.y,Math.PI/6))
                    context.NodeList.push(new Bullet(this.x,this.y,-Math.PI/6))
                }
                break;
            }
            case 4 :{
                if(this.time%this.buttelRate==0){
                    context.NodeList.push(new Bullet(this.x,this.y))
                    context.NodeList.push(new Bullet(this.x,this.y,Math.PI/6))
                    context.NodeList.push(new Bullet(this.x,this.y,-Math.PI/6))
                    context.NodeList.push(new Bullet(this.x,this.y,Math.PI))
                }
            }
            case 5 :{
                if(this.time%this.buttelRate==0){
                    context.NodeList.push(new Bullet(this.x,this.y))
                    context.NodeList.push(new Bullet(this.x,this.y,Math.PI/2))
                    context.NodeList.push(new Bullet(this.x,this.y,-Math.PI/2))
                    context.NodeList.push(new Bullet(this.x,this.y))
                    context.NodeList.push(new Bullet(this.x,this.y,Math.PI/6))
                    context.NodeList.push(new Bullet(this.x,this.y,-Math.PI/6))
                    context.NodeList.push(new Bullet(this.x,this.y,Math.PI))
                }
            }
        }
        return context
    }
    //状态判断
    isOK(context){
        if(this.hp<=0){
            context.gameOver = true
            if(!this.timer){
                setTimeout(()=>{
                    this.OK = false
                },2000)
            }
        }
        return context
    }
}

export default PlayEnity